package com.example.administrator.myapplication;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.example.administrator.myapplication.chat.Print;
import com.example.administrator.myapplication.chat.adapter.WebAdapter;
import com.example.administrator.myapplication.chat.classstring.ChatRecord;
import com.example.administrator.myapplication.chat.interface_.MessageReceive;
import com.example.administrator.myapplication.chat.uri.H_Uri;
import com.neovisionaries.ws.client.WebSocket;
import com.neovisionaries.ws.client.WebSocketFactory;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private EditText editText,editText1,editText2,editText3;
    private Button button;
    private Button button2;
    private Button button3;
    private TextView textView;

    WebSocket webSocket;
    URI url;
    public MainActivity mainActivity;
    public MainActivity (){
        mainActivity=this;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editText=findViewById(R.id.editText);
        editText1=findViewById(R.id.editText1);
        editText2=findViewById(R.id.editText2);
        editText3=findViewById(R.id.editText3);
        button=findViewById(R.id.button);
        button2=findViewById(R.id.button2);
        button3=findViewById(R.id.button3);
        textView=findViewById(R.id.textView);

        button.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);

        mainActivity=this;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button:
                log();
                break;
            case R.id.button2:
                mage();
                break;
            case R.id.button3:
                startActivity(new Intent(getBaseContext(),Mss.class));
                finish();
                break;
        }
    }
    void log(){
        String s= H_Uri.login+ "name="+editText.getText().toString()+"&password="+editText3.getText().toString();
//        "name="+editText.getText().toString()+ "&" +
        try {
            url=new URI(s);
            webSocket=new WebSocketFactory().createSocket(url,5000)
                    .setFrameQueueSize(5)
                    .setMissingCloseFrameAllowed(false)
                    .addListener(new WebAdapter(iin))
                    .connectAsynchronously();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
    void mage(){
        RequestParams params=new RequestParams(H_Uri.senMessage);
        params.addParameter("myid",editText.getText().toString());
        params.addParameter("id",editText1.getText().toString());
        params.addParameter("type","1");
        params.addParameter("data",editText2.getText().toString());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
//                textView.setText(result);
                Print.string("__"+result);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Print.string(ex.toString());
            }

            @Override
            public void onCancelled(CancelledException cex) {
                Print.string(cex.toString());
            }

            @Override
            public void onFinished() {

            }
        });
    }

   public MessageReceive iin=new MessageReceive() {
       @Override
       public void messageLogin(String string) {
           textView.setText(string);
       }

       @Override
       public void messageFriends(String string) {


           ChatRecord chatRecord= JSON.parseObject(string,ChatRecord.class);
           Print.string(chatRecord.toString());
           Print.string(chatRecord.getChatContent());
           textView.setText(chatRecord.getChatContent());
       }

       @Override
       public void message(String string) {
           textView.setText(string);
       }

       @Override
       public void messageSystem(String string) {
           textView.setText(string);
       }
   };

}
